import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Student {
    private String name;
    private String id;
    private String course;
    private double score;

    public Student(String name, String id, String course, double score) {
        this.name = name;
        this.id = id;
        this.course = course;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public String getCourse() {
        return course;
    }

    public double getScore() {
        return score;
    }
}

public class StudentScoreManagementSystem {
    private static List<Student> students = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("=" + "=".repeat(30));
            System.out.println("欢迎使用学生成绩管理系统");
            System.out.println("=" + "=".repeat(30));
            System.out.println("请选择操作：");
            System.out.println("1. 记录学生成绩");
            System.out.println("2. 查询学生成绩");
            System.out.println("3. 统计课程成绩");
            System.out.println("4. 退出系统");
            System.out.print("请输入选项序号：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    recordScore();
                    break;
                case 2:
                    queryScore();
                    break;
                case 3:
                    statisticScore();
                    break;
                case 4:
                    System.out.println("感谢使用，系统退出！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入");
            }
        }
    }

    private static void recordScore() {
        while (true) {
            System.out.print("请输入学生姓名：");
            String name = scanner.nextLine();
            System.out.print("请输入学生学号：");
            String id = scanner.nextLine();
            System.out.print("请输入课程名称：");
            String course = scanner.nextLine();

            try {
                System.out.print("请输入成绩（0 - 100分）：");
                double score = scanner.nextDouble();
                scanner.nextLine(); // 消耗换行符

                if (score < 0 || score > 100) {
                    throw new IllegalArgumentException("成绩应在0 - 100之间");
                }

                for (Student student : students) {
                    if (student.getId().equals(id)) {
                        throw new IllegalArgumentException("学号已存在");
                    }
                }

                students.add(new Student(name, id, course, score));
                System.out.println("成绩记录成功！");
                break;
            } catch (IllegalArgumentException e) {
                System.out.println("错误：" + e.getMessage() + "，请重新输入。");
            }
        }
    }

    private static void queryScore() {
        System.out.println("请选择查询方式：");
        System.out.println("1. 按学生姓名查询");
        System.out.println("2. 按学生学号查询");
        System.out.println("3. 按课程名称查询");
        System.out.print("请输入选项序号：");

        int queryChoice = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        switch (queryChoice) {
            case 1:
                System.out.print("请输入学生姓名：");
                String name = scanner.nextLine();
                boolean foundByName = false;
                for (Student student : students) {
                    if (student.getName().equals(name)) {
                        System.out.printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f%n",
                                student.getName(), student.getId(), student.getCourse(), student.getScore());
                        foundByName = true;
                    }
                }
                if (!foundByName) {
                    System.out.println("未找到该学生记录");
                }
                break;
            case 2:
                System.out.print("请输入学生学号：");
                String id = scanner.nextLine();
                boolean foundById = false;
                for (Student student : students) {
                    if (student.getId().equals(id)) {
                        System.out.printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f%n",
                                student.getName(), student.getId(), student.getCourse(), student.getScore());
                        foundById = true;
                    }
                }
                if (!foundById) {
                    System.out.println("未找到该学生记录");
                }
                break;
            case 3:
                System.out.print("请输入课程名称：");
                String course = scanner.nextLine();
                boolean foundByCourse = false;
                for (Student student : students) {
                    if (student.getCourse().equals(course)) {
                        System.out.printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f%n",
                                student.getName(), student.getId(), student.getCourse(), student.getScore());
                        foundByCourse = true;
                    }
                }
                if (!foundByCourse) {
                    System.out.println("未找到该课程的成绩记录");
                }
                break;
            default:
                System.out.println("无效的查询选项");
        }
    }

    private static void statisticScore() {
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();

        List<Double> scores = new ArrayList<>();
        for (Student student : students) {
            if (student.getCourse().equals(course)) {
                scores.add(student.getScore());
            }
        }

        if (!scores.isEmpty()) {
            double sum = scores.stream().mapToDouble(Double::doubleValue).sum();
            double avg = sum / scores.size();
            double max = scores.stream().mapToDouble(Double::doubleValue).max().getAsDouble();
            double min = scores.stream().mapToDouble(Double::doubleValue).min().getAsDouble();

            System.out.printf("课程：%s%n", course);
            System.out.printf("平均分：%.2f%n", avg);
            System.out.printf("最高分：%.1f%n", max);
            System.out.printf("最低分：%.1f%n", min);
        } else {
            System.out.println("未找到该课程的成绩记录");
        }
    }
}